<template>
  <div class="register-container">
    <div class="register-background"></div>
    <el-card class="register-card">
      <h2 class="register-title">注册</h2>
      <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" label-position="left">
        <el-form-item label="账号" prop="account">
          <el-input v-model="registerForm.account" placeholder="请输入账号"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="registerForm.password" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱"/>
        </el-form-item>
        <el-form-item label="资格证书" prop="certifications">
          <el-upload
              class="avatar-uploader"
              :show-file-list="false"
              accept=".png,.jpg,.jpeg,.gif"
              drag
              :before-upload="handleBeforeUpload"
              :http-request="handleCustomUploadReport"
              multiple
          >
            <img style="max-width: 100%; height: auto;" v-if="registerForm.certifications"
                 :src="registerForm.certifications" class="avatar" alt="资格证书"/>
            <el-icon v-else class="avatar-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="register-actions">
        <el-button type="primary" class="register-button" @click="handleregister">注册</el-button>
      </div>
      <div class="register-footer">
        已经有账号？<a @click="goLogin">立即登录</a>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {reactive, ref} from 'vue';
import {ElForm, ElFormItem, ElInput, ElButton, ElMessage} from 'element-plus';
import {useRouter} from "vue-router";
import {Plus} from "@element-plus/icons-vue";
import {apiPost} from "@/utils/api";

//获取el-form组件
let registerFormRef = ref()
//获取路由器
let $router = useRouter()

const registerForm = reactive({
  account: '111111',
  password: '111111',
  email: '1586156397@qq.com',
  certifications: '',
});

const registerRules = {
  account: [
    {required: true, message: '请输入账号', trigger: 'blur'},
    {min: 6, message: '账号长度不能少于6位', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, message: '密码长度不能少于6位', trigger: 'blur'}
  ],
  email: [
    {required: true, message: '请输入邮箱', trigger: 'blur'},
    {type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur'}
  ],
  certifications: [
    {required: true, message: '请上传资格证书', trigger: 'blur'}
  ]
};

const handleregister = async () => {
  await registerFormRef.value.validate()
  await apiPost('/trainers/register', {...registerForm});
  await $router.push({path: '/login'})
};

//上传文件之前做的一些判断
const handleBeforeUpload = (file) => {
  // 检查文件类型 只能上传.png,.jpg,.jpeg,.gif
  const isExcel = file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/jpeg' || file.type === 'image/gif';
  if (!isExcel) {
    ElMessage.error('上传文件只能是.png,.jpg,.jpeg,.gif 格式!');
    return false;
  }

  //上传文件大小不能超过20M
  const isLt20M = file.size / 1024 / 1024 < 20;
  if (!isLt20M) {
    ElMessage.error('上传文件大小不能超过 20MB!');
    return false;
  }
  return isExcel && isLt20M;
};
//导入资格证书文件事件
const handleCustomUploadReport = async (option) => {
  const formData = new FormData();
  formData.append('file', option.file);
  let {msg} = await apiPost('/upload/img', formData, {headers: {'Content-Type': 'multipart/form-data'}});
  registerForm.certifications = msg;
};

//跳转到登录页面
const goLogin = () => {
  $router.push({path: '/login'})
}
</script>
<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  overflow: hidden;

  .register-background {
    position: fixed; /* 使用固定定位 */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url('https://via.placeholder.com/1920x1080/09f/fff') no-repeat center center;
    background-size: cover;
    z-index: -1;
  }

  .register-card {
    width: 400px;
    padding: 30px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    text-align: center;
  }

  .register-title {
    margin-bottom: 30px;
  }

  .register-actions {
    display: flex;
    justify-content: center;
  }

  .register-button {
    width: 100%; /* 使按钮宽度与卡片宽度一致 */
  }
}
</style>